package com.zj.common.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

//鏂囦欢涓婁紶涓嬭浇鍏叡鎻掍欢
public class UploadController {

	

    public static String uploadFile(MultipartFile file){  
        //鑾峰彇涓婁紶鏂囦欢鐨勫悕绉�  
		
        String filePath = file.getOriginalFilename();
        Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss")  
                .format(new Date()));  
        Long random = (long) (Math.random() * now);  
        String fileName = now + "" + random;  
  
        if (filePath.indexOf(".") != -1) {  
            fileName =fileName+"="+filePath;  
        }  
        
        String path="E:/upload";
        
        File file2 = new File(path);  
        if (!file2.exists()) {  
            //鍒涘缓涓存椂鐩綍  
            file2.mkdir();  
        }  
        try {  
            //鏂囦欢瀛樻斁鐨勮矾寰�  
            FileOutputStream fileOutputStream = new FileOutputStream(file2+"/"+fileName);  
            fileOutputStream.write(file.getBytes());  
            System.out.println(path);
            fileOutputStream.flush();  
            fileOutputStream.close(); 
            
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
            return "error";  
        } catch (IOException e) {  
            e.printStackTrace();  
            return "error";  
        }  
      
        return fileName;   
    }  
    
    
    public static void downloadFile(HttpServletRequest request,String filekey,HttpServletResponse response){    
       response.setCharacterEncoding("utf-8");    
       response.setContentType("multipart/form-data"); 
       String []fillist= filekey.split("=");
       String filename="";
       if(fillist.length>=2){    	   
    	   for(int i=1;i<fillist.length;i++){
    		   filename=filename+fillist[i];
    	   }
       }
   
       try {
		response.setHeader( "Content-Disposition", "attachment;filename=" + new String( filename.getBytes("gb2312"), "ISO8859-1" ) );
	} catch (UnsupportedEncodingException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
       try {    
           File file=new File(filekey);
           String path="E:/upload";
           
           InputStream inputStream=new FileInputStream(path+"/"+file);    
           OutputStream os=response.getOutputStream();    
           byte[] b=new byte[1024];    
           int length;    
           while((length=inputStream.read(b))>0){    
               os.write(b,0,length);    
           }    
           inputStream.close(); 
           os.close();
       } catch (FileNotFoundException e) {    
           e.printStackTrace();    
       } catch (IOException e) {    
           e.printStackTrace();    
       }    
   }    
}
